package com.dzu.medium;

import java.util.Arrays;

/**
 * @author ZhaoDong
 * @date 2023/3/3 11:28
 * @description 59. 螺旋矩阵 II
 */
public class Test59 {
    public static void main(String[] args) {
        System.out.println(Arrays.deepToString(generateMatrix(1)));
    }

    public static int[][] generateMatrix(int n) {
        int i, j;
        int x = 0, y = 0;
        int count = 1;
        int offset = 1;
        int[][] resp = new int[n][n];
        int loop = n / 2;
        while (loop > 0) {
            loop--;
            for (j = y; j < n - offset; j++) {
                resp[x][j] = count++;
            }
            for (i = x; i < n - offset; i++) {
                resp[i][j] = count++;
            }
            for (; j > y; j--) {
                resp[i][j] = count++;
            }
            for (; i > x; i--) {
                resp[i][j] = count++;
            }
            x++;
            y++;
            offset++;
        }
        if (n % 2 == 1) {
            resp[x][y] = count;
        }

        return resp;
    }
}
